import APIClient from "../../network/index"

const classfyStore = {
    state: {
        navList:[], 
        fristList:[],
        secondList:[],
        detailsList:[],
        goodsDetails:{}
    },
    mutations: {
        get_goods_nav(state,goods){
            state.fristList=[],
            state.secondList=[],
            state.navList = [...goods],
            state.fristList.push({"name":"所有分类"})
            goods.forEach((item)=>{
                if(item.pid === 0 ){
                    state.fristList.push(item)
                } else {
                    state.secondList.push(item)
                }
            })
        },
        get_change_nav(state,goods){
            state.secondList=[];
            if(goods.name == "所有分类"){
                state.navList.forEach((item)=>{
                    if(item.pid!=0){
                        state.secondList.push(item)
                    }
                })
            }else {
                state.navList.forEach((item)=>{
                    if(item.pid == goods.id){
                        state.secondList.push(item)
                    }
                })
                }
        },

        getDetailsList(state,goods){
            state.detailsList = []
                goods.list.forEach((item)=>{
                    if(item.categoryId == goods.id){
                        state.detailsList.push(item)
                    }
                })
        },

        get_goods_details(state,goods){
            state.goodsDetails = goods
        }
    },
    actions: {
        get_goods_nav(context){
            APIClient.get_goods_nav().then((res)=>{
                context.commit("get_goods_nav",res.data.data)
            })
        },
        get_change_nav(context,goods){
                context.commit("get_change_nav",goods)
        
        },
        getDetailsList(context,id){
            APIClient.getStoreList().then((res)=>{
                context.commit("getDetailsList",{
                    "list":res.data.data,
                    "id":id
                })
            })
        },
        get_goods_details(context,id){
            APIClient.get_shop_details({
                params:{
                    id
                }
            }).then((res)=>{
                context.commit("get_goods_details",res.data.data)
            })

            APIClient.get_goods_evaluate({
                params:{
                    id
                }
            }).then((res)=>{
                console.log(res.data)
            })
        }
    }
}
export default classfyStore